package cn.timeapi.example;

import cn.timeapi.sdk.data.Response;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 描述:提取地址信息
 *  解析例子：张三 18888888888 福建省厦门市集美区xxx号 000000
 * @author Vincent Vic
 * version 0.1
 * create 2020-03-08 13:25
 */

public class AddressResolutionUtil {

    /**
     * 本例子实现地址解析主方法 addressResolution Data字段为Map类型
     * 从本方法拓展Data字段为List和自定义类例子
     */

    /**
     * 解析地址  Data 设置成Map
     * @author lin
     * @param address
     * @return
     */
    public static Response addressResolution(String address){
        /**
         * +
         *                 "?(?<postal>\\d{6})"+
         */
        //去掉常见符号
        address = address.replace(",", " ");
        address = address.replace("，", " ");
        address = address.replace("、", " ");
        address = address +" ";
        Response  result = new Response();
        if (address==null){
            result.setCode("-1");
            result.setMsg("参数为空");
            return result;
        }
        Map<String,String> row=new LinkedHashMap<String,String>();
        String phone=null,postal=null,name=null;
        String regex = "(1[3578]\\d{9})";
        Matcher m=Pattern.compile(regex).matcher(address);
        //取出电话
        if (m.find()) {
            phone = m.group();
            row.put("phone", phone == null ? "" : phone.trim());
            address = address.replace(phone, "");
        }else{
            row.put("phone","");
        }
        //取出邮编
        regex = "(\\d{6})";
        m=Pattern.compile(regex).matcher(address);
        if (m.find()) {
            postal = m.group();
            row.put("postal", postal == null ? "000000" : postal.trim());
            address = address.replace(postal, "").trim();
        }else{
            row.put("postal","");
        }

        //取出姓名(?:·[\u4E00-\u9FA5]{2,4})*
        regex = "[\\s]?[\u4E00-\u9FA5]{2,4}[\\s]";
        m=Pattern.compile(regex).matcher(address);
        if (m.find()) {
            name = m.group();
            row.put("name", postal == null ? "" : name.trim());
            address = address.replace(name, "").trim();
        }else{
            row.put("name","");
        }
        //得到地址
        regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)" +
                "(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)" +
                "(?<county>[^县]+县|.*?区|.*?市|.*?旗|.*?海域|.*?岛)" +
                "?(?<town>[^区]+区|.*?镇|.*?社区|.*?街道|.*?乡)" +
                "?(?<village>.*)";
        m=Pattern.compile(regex).matcher(address);
        String province=null,city=null,county=null,town=null,village=null;
        while(m.find()){

            province=m.group("province");
            row.put("province", province==null?"":province.trim());
            city=m.group("city");
            row.put("city", city==null?"":city.trim());
            county=m.group("county");
            row.put("county", county==null?"":county.trim());
            town=m.group("town");
            row.put("town", town==null?"":town.trim());
            village=m.group("village");
            row.put("village", village==null?"":village.trim());
            result.setData(row);

        }
        if (result.getData() != null ){
            result.setCode("0");
            result.setMsg("success");
        }else{
            result.setCode("404");
            result.setMsg("提取信息失败或数据有误");
        }

        return result;
    }

    /**
     * 解析地址  Data 设置成Map
     * @author lin
     * @param address
     * @return
     */
    public static Response addressResolutionSetList(String address){

        List<String> list = new ArrayList<String>();
        //这里避免重复逻辑，复用初始解析方法，改变返回值为List类型
        Response result = addressResolution(address);
        Map<String,String> map = (Map<String,String>)result.getData();
        for (Map.Entry entry : map.entrySet()){
            list.add((String) entry.getValue());
        }
        result.setData(list);
        return result;
    }

    /**
     * 解析地址  Data 设置成自定义类型
     * @author lin
     * @param address
     * @return
     */
    public static Response addressResolutionSetInfo(String address){

        Addressinfo info=new Addressinfo();
            //这里避免重复逻辑，复用初始解析方法，改变返回值为List类型
        Response result = addressResolution(address);
        Map<String,String> map = (Map<String,String>)result.getData();
        for (Map.Entry entry : map.entrySet()){
            //通过反射设置值
            String key = (String) entry.getKey();
            String setMethodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1);
            try {
                Method method = Addressinfo.class.getMethod(setMethodName,String.class);
                method.invoke(info,(String)entry.getValue());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        result.setData(info);

        return result;
    }

}